'''
Created on Sep 29, 2010

@author: oabalbin
'''

'''
hsa-let-7e_MIMAT0000066_Homo_sapiens_let-7e     164830  hsa-let-7e_MI0000066_Homo_sapiens_let-7e_stem-loop

read files in folder
parse the file
field[0] correspond to mirna name
second colum to the counts 
third column to the hairpin loop. 
'''

import os
import sys
import glob
import numpy as np
from optparse import OptionParser
from collections import defaultdict,deque

def read_files_folder(folderpath,ext):
    ''' '''
    # Read files in folder
    myfiles=deque()
    
    for infile in glob.glob( os.path.join(folderpath, '*'+ext) ):
        filename=infile.split('/')[-1]
        myfiles.append(infile)
        #print "current file is: " + filename
    return myfiles

def parse_expression_file(inputfile_name, mirna_dictionary, findex, zeroslist):
    
    inputfile = open(inputfile_name)
    
    for line in inputfile:
        if line.startswith('#'): 
            continue
        fields = line.strip('\n').split('\t')
        mature_name, precursor_name = fields[0], fields[2].split('_')[0]
        key_name=mature_name+'_'+precursor_name
        
        if findex==0:
            mirna_dictionary[key_name].append(fields[1])
        else:
            if key_name in mirna_dictionary:
                mirna_dictionary[key_name].append(fields[1])
            else:
                mirna_dictionary[key_name].append(0)
        
    return mirna_dictionary
    

def read_logfile(inputfile_name):
    
    sample_label = defaultdict()
    inputfile = open(inputfile_name)
    for line in inputfile:
        fields = line.strip('\n').split(' ')
        spname =  fields[0].strip('../').split('_')[0]
        print spname, fields[-2]
        sample_label[fields[-2]] = spname
    
    return sample_label


if __name__ == '__main__':
    
    optionparser = OptionParser("usage: %prog [options] ")
    optionparser.add_option("-d", "--folder", dest="folder",
                            help="Path to the folder that contains the .tsv files")
    optionparser.add_option("-s", "--logfile", dest="logfile",
                            help="Path to the samples log file")

    
    (options, args) = optionparser.parse_args()
    
    folderpath = options.folder
    logfile_name= options.logfile
    outfile = open(folderpath+'mirna_composite.tmp','w')
    
    sample_label = read_logfile(logfile_name)
    
    #sys.exit(0)
    
    myfiles = read_files_folder(folderpath,'.csv')
    expr_vect = list(np.zeros(len(myfiles)))
    mirna_dictionary = defaultdict(deque)
    allsamples = deque()
    
    for findex, inputfile_name in enumerate(myfiles):
        print inputfile_name
        thisfile = inputfile_name.strip('_tag.csv').split('/miRNAs_expressed_')[-1]
        spname = sample_label[thisfile]
        allsamples.append(spname) 
        mirna_dictionary = parse_expression_file(inputfile_name, mirna_dictionary, findex, expr_vect)
    
    header = [name.split('/')[-1] for name in allsamples]
    outfile.write('mirna_name'+'\t'+",".join(header).replace(',','\t')+'\n')
    #sorted_mirnas = mirna_dictionary.keys()
    
    for mirna in sorted(mirna_dictionary.keys()):
    #for mirna, expr_counts in mirna_dictionary.iteritems():
        expr_counts = mirna_dictionary[mirna]
        outfile.write(mirna+'\t'+",".join(map(str,expr_counts)).replace(',','\t')+'\n')
        
    
    
    
    
